<?php
/**
 * Created by PhpStorm.
 * User: YBF
 * Date: 2018/1/14
 * Time: 11:18
 */

namespace system\route;
defined('WWW_ROOT') or die(header("HTTP/1.1 404 Not Found"));

class Route
{
    private $controller;
    private $action;
    //获取当前上面的所有参数

    /**
     * route constructor.
     */
    public function __construct()
    {
        $this->analysisUrl();
    }

    public function analysisUrl(){
        //解析url
        if (!isset($_REQUEST['c']) || $_REQUEST['c'] == '') {
            die('没有找到控制器');
        } else {
            $this->setController(str_replace('\.','\\',trim($_REQUEST['c'])));
        }
        if (!isset($_REQUEST['do']) || $_REQUEST['c'] == '') {
            die('没有找到控制器方法');
        } else {
            $this->setAction(trim($_REQUEST['do']));
        }
        $controllerName = 'app\\Controller\\' . $this->getController() . 'Controller';
        $doName = $this->getAction();
        try {
            $controller = new $controllerName;
            if (method_exists($controllerName, $doName)) {
                $actionBack = $controller->$doName();
                if ($actionBack != ''){
                    echo json_encode($actionBack);
                }
            } else {
                die('method Unable to load');
            }
        } catch (\Exception $e) {
            echo $e->getMessage(), "\n";
        }
    }

    public function getController()
    {
        return $this->controller;
    }

    public function setController($controller)
    {
        $this->controller = $controller;
    }

    public function getAction()
    {
        return $this->action;
    }

    public function setAction($action)
    {
        $this->action = $action;
    }
}